package org.jeecg.modules.demo.crm.mapper;

import java.math.BigDecimal;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.jeecg.modules.demo.crm.entity.CInvoiceApply;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.jeecg.modules.demo.crm.entity.CPaymentInvoice;

/**
 * @Description: 开票任务表
 * @Author: jeecg-boot
 * @Date:   2025-06-13
 * @Version: V1.0
 */
public interface CInvoiceApplyMapper extends BaseMapper<CInvoiceApply> {

    @Select("select IFNULL(sum(total_amount),0) as totla from c_invoice_apply  where project_id=#{projectId}  ")
    BigDecimal getTotalAmount(@Param("projectId") String projectId);

    @Select("select IFNULL(sum(total_tax_amount),0) as totla from c_invoice_apply  where project_id=#{projectId}  ")
    BigDecimal getTotalTaxAmount(@Param("projectId") String projectId);


    /**
     * 开票合计
     * @param projectId
     * @return
     */
    @Select("select IFNULL(sum(total_amount_with_tax),0) as totla from c_invoice_apply  where project_id=#{projectId}  ")
    BigDecimal getTotalAmountWithTax(@Param("projectId") String projectId);


    @Select("select * from c_invoice_apply where invoice_number =#{invoiceNumber}")
    CInvoiceApply findByInvoiceNumber(@Param("invoiceNumber")String invoiceNumber);

    @Select("select * from c_invoice_apply")
    List<CInvoiceApply> findiInvoiceAppSplit();

    @Select("select  IFNULL(sum(t1.total_amount_with_tax),0) from c_invoice_apply as t1 LEFT JOIN c_payment_record as t2 on t1.id = t2.invoice_apply_id where  t2.id IS NULL and t1.project_id=#{projectId}  ")
    BigDecimal totalTeceivables(@Param("projectId") String projectId);


    @Select("<script> " +
            "SELECT IFNULL(SUM(total_amount_with_tax), 0) AS total " +
            "FROM c_invoice_apply " +
            "WHERE id IN " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'> #{id} </foreach>" +
            "</script>")
    BigDecimal getTotalAmountWithTaxByIds(@Param("ids") List<String> ids);

}
